/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpart;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.tools.DirectEditManager;

import ro.pub.cs.se.moondance.editor.figures.RegionFigure;
import ro.pub.cs.se.moondance.editpolicy.RegionDeleteEditPolicy;
import ro.pub.cs.se.moondance.editpolicy.RegionLayoutEditPolicy;
import ro.pub.cs.se.moondance.editpolicy.UMLNodeEditPolicy;
import ro.pub.cs.se.moondance.model.UMLEntity;
import ro.pub.cs.se.moondance.model.UMLRegion;

public class RegionEditPart extends UMLEntityEditPart implements NodeEditPart {
	protected DirectEditManager manager;
	private ConnectionAnchor anchor;

	// ******************* Life-cycle related methods *********************/

	/**
	 * @see org.eclipse.gef.EditPart#activate()
	 */
	public void activate() {
		super.activate();
	}

	/**
	 * @see org.eclipse.gef.EditPart#deactivate()
	 */
	public void deactivate() {
		super.deactivate();
	}

	// ******************* Model related methods *********************/

	/**
	 * Returns the Class model object represented by this EditPart
	 */
	public UMLRegion getRegionModel() {
		return (UMLRegion) getModel();
	}

	/**
	 * @return the children Model objects as a new ArrayList
	 */
	protected List<UMLEntity> getModelChildren() {
		ArrayList<UMLEntity> list = new ArrayList<UMLEntity>(getRegionModel()
				.getStates());
		list.addAll(getRegionModel().getStates());
		return list;
	}

	// ******************* Editing related methods *********************/

	/**
	 * Creates edit policies and associates these with roles
	 */
	protected void createEditPolicies() {
		// installEditPolicy(EditPolicy.CONTAINER_ROLE, new
		// ClassContainerEditPolicy());
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new RegionLayoutEditPolicy());
		// installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new
		// RegionDirectEditPolicy());
		installEditPolicy(EditPolicy.COMPONENT_ROLE,
				new RegionDeleteEditPolicy());
		installEditPolicy(EditPolicy.GRAPHICAL_NODE_ROLE,
				new UMLNodeEditPolicy());

	}

	// ******************* Direct editing related methods *********************/

	/**
	 * @see org.eclipse.gef.EditPart#performRequest(org.eclipse.gef.Request)
	 */

	// ******************* Miscellaneous stuff *********************/

	/**
	 * @see org.eclipse.gef.editparts.AbstractEditPart#toString()
	 */
	public String toString() {
		return getModel().toString();
	}

	// ******************* Listener related methods *********************/

	/**
	 * Handles change in name when committing a direct edit
	 */
	/*
	 * protected void commitNameChange(PropertyChangeEvent evt) { PackageFigure
	 * packageFigure = (PackageFigure) getFigure(); EditableLabel label =
	 * packageFigure.getNameLabel();
	 * label.setText(getPackageModel().getLabelText()); label.setVisible(true);
	 * refreshVisuals(); }
	 */
	// ******************* Layout related methods *********************/

	public IFigure getContentPane() {
		RegionFigure figure = (RegionFigure) getFigure();
		return figure.getMainFigure();
	}

	protected ConnectionAnchor getConnectionAnchor() {
		if (anchor == null) {
			anchor = new ChopboxAnchor(getFigure());
		}
		return anchor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef
	 * .ConnectionEditPart)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getSourceConnectionAnchor(org.eclipse.gef
	 * .Request)
	 */
	public ConnectionAnchor getSourceConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef
	 * .ConnectionEditPart)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(
			ConnectionEditPart connection) {
		return getConnectionAnchor();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.NodeEditPart#getTargetConnectionAnchor(org.eclipse.gef
	 * .Request)
	 */
	public ConnectionAnchor getTargetConnectionAnchor(Request request) {
		return getConnectionAnchor();
	}

	/**
	 * Sets the width of the line when selected
	 */
	public void setSelected(int value) {
		super.setSelected(value);/*
								 * RegionFigure regionFigure = (RegionFigure)
								 * getFigure(); if (value !=
								 * EditPart.SELECTED_NONE)
								 * regionFigure.setSelected(true); else
								 * regionFigure.setSelected(false);
								 * regionFigure.repaint();
								 */
	}

}
